package com.nacrtstudio.webgiftshop.pages;

import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.tynamo.security.services.SecurityService;

import com.nacrtstudio.webgiftshop.facade.Controller;
import com.nacrtstudio.webgiftshop.model.shop.Address;
import com.nacrtstudio.webgiftshop.model.shop.Cart;
import com.nacrtstudio.webgiftshop.model.shop.User;
import com.nacrtstudio.webgiftshop.model.shop.beans.AddressBean;
import com.nacrtstudio.webgiftshop.model.shop.beans.CartBean;
import com.nacrtstudio.webgiftshop.model.shop.beans.UserBean;
import com.nacrtstudio.webgiftshop.util.shop.Country;
import com.nacrtstudio.webgiftshop.util.shop.Role;

public class Registration {
	@Inject
	private Request request;
	public void onActivate() {
		try {
			if (controller.getRequest()==null) {
	    		controller.setRequest(request);
			}
			if(user==null) user = new UserBean();
		} catch (Exception e) {
		}
	}
	
	@Property
	@Persist
	private User user;
	
	@Inject
	@Property
	private Controller controller;
	
	@Inject
	private SecurityService securityService;
	
	@InjectComponent
	private Form registrationForm;
	
	@InjectPage
	private StatusPage statusPage;
	
	@Property
	private String email;
	@Property
	private String username;
	@Property
	private String password;
	@Property
	private String rePassword;
	@Property
	private boolean acceptTAC;	
	@Log
	public Object onSubmitFromRegistrationForm(){
		try {
			if (acceptTAC) {
				if (controller.getUserByUsername(username)==null) {
					User newUser  = new UserBean();
					Subject currentUser = securityService.getSubject();
					newUser.getRoles().add(Role.enduser);
					if (controller.getAllUsers().size()==0) {
						newUser.getRoles().add(Role.admin);
					}
					if (password.equals(rePassword)) {
						Address address = new AddressBean();
						address.setCountry(Country.Serbia_and_Montenegro);
						newUser.setAddress(address);
						Cart cart = new CartBean();
						newUser.setCart(cart);
						newUser.seteMail(email);
						newUser.setUsername(username);
						newUser.setPassword(password);
						controller.updateUser(newUser);
					}
					statusPage.setMessage("Uspešno ste se registrovali!");
					controller.login(newUser.getUsername(), newUser.getPassword());
					UsernamePasswordToken token = new UsernamePasswordToken(newUser.getUsername(), newUser.getPassword());
					currentUser.login(token);
					return statusPage;
				}
				statusPage.setMessage("Vase korisnicko ime je zauzeto");
				return statusPage;
			}
		} catch (Exception e) {
			statusPage.setMessage("Šifre koje ste uneli se ne poklapaju. Pokušajte ponovo!");
			return statusPage;
		}
		statusPage.setMessage("Morate da prihvatite uslove koriscenja!");
		return statusPage;
	}
}
